
package com.beans.indicator.view;

import com.beans.indicator.model.IndicatorModel;
import java.awt.*;
import java.awt.geom.Line2D;


/**
 *
 * @author FreeFrag
 */
public class HorizontalIndicatorView extends AbstractIndicatorView {
    
    private Color diodeOn = new Color(255,0,51);
    private Color diodeOff = new Color(153,0,51);
    
    public HorizontalIndicatorView(IndicatorModel model) {
        super(model);
        background = Color.GRAY;
        largeTickInterval=10;
        diodeVisible = true;
        
        this.setPreferredSize(new Dimension(350,150));
        
    }

    @Override
    protected void paintIndicatorFace(Graphics g) {
        
        int borderX = getWidth()/7, borderY = getHeight()/3;
        double allDistance = (getWidth()-borderX/2-borderX/4)-(borderX/2+borderX/4);
        double x = 0;
        int startValue=model.getMinValue();
        int endValue=model.getMaxValue();
        int Value=endValue-startValue;
        float tickNumberX = (float)(float)borderX/2+borderX/10;
        double dist = allDistance/tickCount;
        
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(foreground);
	g2.fillRect(5, 5, getWidth()-10, getHeight()-10);
        g2.setColor(background);
        g2.fillRect(borderX/2, borderY/2, getWidth()-borderX, getHeight()-borderY);
	g2.setColor(Color.black);
	g2.drawRect(4, 4, getWidth()-8, getHeight()-8);
        g2.setColor(foreground);
	g2.fillRect(borderX/2+borderX/4, getHeight()/3+getHeight()/13, (int)allDistance, borderY/2);
        
        for (int i=0; i<=tickCount; i++) {
            
            if (i%largeTickInterval==0){
                g2.setColor(tickColor);
                Line2D.Double myline = new Line2D.Double(borderX/2+borderX/4+x, getHeight()/2+borderY/3, borderX/2+borderX/4+x, getHeight()-getHeight()/3.5);
                g2.draw(myline);
                Font f = this.getFont();
                g2.setFont(new Font(f.getFontName(), Font.PLAIN, getHeight()/13));
                g2.setColor(labelColor);
                if (i!=tickCount)
                {
                    g2.drawString(""+startValue, tickNumberX+(float)x, (float)(getHeight()-getHeight()/4.5));   
                }else
                    g2.drawString(""+endValue, tickNumberX+(float)x, (float)(getHeight()-getHeight()/4.5));   
                startValue=startValue+(Value/(tickCount/largeTickInterval));
            }if (i%(largeTickInterval/2)==0){
                g2.setColor(tickColor);
                Line2D.Double myline = new Line2D.Double(borderX/2+borderX/4+x, getHeight()/2+borderY/3, borderX/2+borderX/4+x, getHeight()-getHeight()/3.3);
                g2.draw(myline);
            }else{
                Line2D.Double myline = new Line2D.Double(borderX/2+borderX/4+x, getHeight()/2+borderY/3, borderX/2+borderX/4+x, getHeight()-getHeight()/3);
                g2.draw(myline);
            }
            x=x+dist;
        }
    }

    @Override
    protected void paintIndicator(Graphics g) {
        
        int distance = (getWidth()-(getWidth()/10)/2-(getWidth()/10)/4)-((getWidth()/10)/2+(getWidth()/10)/4);
        int positionX = ((getWidth()/10)/2+(getWidth()/10)/4)+(distance-((distance*(model.getValuesCount()-model.getAbsoluteValue(model.getValue())))/model.getValuesCount()));
        int positionY = getHeight()/3+getHeight()/13;
        
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(colorIndicator);
        g2.fillRect(positionX-2, positionY, 4, (getHeight()/3)/2);
    }

    @Override
    protected void paintDiode(Graphics g) {
        Graphics2D g2 = getAAInstance(g);
        Color c = null;
        if(model.isValueCritical()) {
                c = diodeOn;
            } else {
                c = diodeOff;
            }
        g2.setColor(c);
        g2.fillOval((getWidth()/6)*5, getHeight()/4, getHeight()/13, getHeight()/13);
    }

    @Override
    protected void paintTitle(Graphics g) {
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(titleColor);
        Font f = this.getFont();
        g2.setFont(new Font(f.getFontName(), Font.PLAIN, getHeight()/10));
        FontMetrics fm = g.getFontMetrics();
        g2.drawString(title, getWidth()/2-fm.stringWidth(title)/2,getHeight()/2 - getHeight()/8);
    }

    @Override
    protected void paintCritical(Graphics g) {
        
        int distance = (getWidth()-( getWidth()/7)/2-(getWidth()/7)/4)-((getWidth()/7)/2+(getWidth()/7)/4);
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(criticalColor);
        
        g2.fillRect(((getWidth()/7)/2+(getWidth()/7)/4)+distance-((distance*(model.getValuesCount()-model.getAbsoluteValue(model.getMaxCriticalValue()))/model.getValuesCount())), getHeight()/3+getHeight()/13,((distance*(model.getValuesCount()-model.getAbsoluteValue(model.getMaxCriticalValue()))/model.getValuesCount())) , getHeight()/3/2);
        g2.fillRect((getWidth()/7)/2+(getWidth()/7)/4, getHeight()/3+getHeight()/13,distance-((distance*(model.getValuesCount()-model.getAbsoluteValue(model.getMinCriticalValue()))/model.getValuesCount())),  getHeight()/3/2);
        
    }
}

